package com.github.customFilter;

import java.util.ArrayList;
import java.util.List;

/**
 * 计数器方式实现的过滤器链
 * Created by zhangcheng03 on 2021-6-29 9:40.
 */
public class FilterChainImpl implements FilterChain {
    List<Filter> listfilter = new ArrayList<Filter>();
    private int index = 0;

    public void append(Filter filter) {
        listfilter.add(filter);
    }

    private boolean execute_target = false;

    @Override
    public void execute() {
        if (index < listfilter.size()) {
            Filter filter = listfilter.get(index);
            index++;
            filter.doFilter(this);
        }
        if (index == listfilter.size()) {
            if (execute_target == false) {
                execute_target = true;
                System.out.println("执行拦截目标的方法");
            }
        }
    }
}